$(function () {

    layui.use(['upload', 'element', 'jquery', 'layer', 'form', 'laydate'], function () {
        var $ = layui.jquery
            , layer = layui.layer;
        let form = layui.form;
        let id = sessionStorage.getItem("applyId");


        //监听提交
        form.on('submit(reply)',
            function (data) {
                data = data.field;
                data.id = sessionStorage.getItem("applyId");
                // console.log(data);
                $.ajax({
                    url: '/back/apply/reply',
                    data: data,
                    type: 'post',
                    dataType: 'json',
                    success: function (res) {
                        if (res.count > 0) {
                            getEmail(id);
                            layer.alert("回复成功，邮件发送成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.alert("回复失败，邮件发送失败",
                                function () {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        }
                    }
                })
                return false;
            });

        function getEmail(id) {
            $.ajax({
                url: '/back/apply/findById',
                data: {id: id},
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    // console.log(res)
                    sendEmail(res.data)
                }
            })
        }

        function sendEmail(data) {
            $.ajax({
                url: '/back/sendEmail',
                data: data,
                type: 'post',
                dataType: 'json',
                success: function (res) {
                    console.log(res)
                    if (res.count === 0) {
                        layer.msg(res.msg);
                    } else {
                        layer.msg(res.msg);
                    }
                }
            })
        }
    })
})